package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.dto.SysUpgradeDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.SysUpgrade;
import net.zigin.nhi.could.waste.service.SysUpgradeService;

import net.zigin.nhi.could.waste.vo.SysUpgradeVo;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SysUpgrade控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUpgradeController
 * @date 2021-9-14 20:30:11
 */
@Api(tags = "版本管理")
@RestController
@RequestMapping("/sysUpgrade")
public class SysUpgradeController extends BaseController {
    @Autowired
    private SysUpgradeService sysUpgradeService;


    /**
     * 新增编辑版本
     *
     * @param sysUpgradeDto 版本信息
     * @return 版本ID
     */
    @ApiOperation("新增编辑版本")
    @ApiImplicitParam(name = "sysUpgradeDto", value = "版本信息", required = true, dataType = "SysUpgradeDto")
    @PostMapping("/save")
    public JsonForResult<String> sava(@RequestBody SysUpgradeDto sysUpgradeDto) {
        String id = sysUpgradeService.saveOrUpdate(sysUpgradeDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 版本列表
     *
     * @param sysUpgradeDto 版本信息
     * @return 版本列表
     */
    @ApiOperation("版本列表")
    @ApiImplicitParam(name = "sysUpgradeDto", value = "版本信息", required = true, dataType = "SysUpgradeDto")
    @PostMapping("/getList")
    public JsonForResult<List<SysUpgradeVo>> getList(@RequestBody SysUpgradeDto sysUpgradeDto) {
        List<SysUpgradeVo> list = sysUpgradeService.getList(sysUpgradeDto);
        Integer count = sysUpgradeService.getCount(sysUpgradeDto);
        return JsonForResult.<List<SysUpgradeVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 版本详情
     *
     * @param id ID
     * @return 版本详情
     */
    @ApiOperation("版本详情")
    @ApiImplicitParam(name = "id", value = "版本id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<SysUpgradeVo> getById(@PathVariable String id) {
        SysUpgrade sysUpgrade = sysUpgradeService.selectByPrimaryKey(id);
        SysUpgradeVo sysUpgradeVo = ReflectUtil.copyProperties(sysUpgrade, SysUpgradeVo.class);
        return JsonForResult.<SysUpgradeVo>builder().data(sysUpgradeVo).success(true).build();
    }

    /**
     * 删除版本
     *
     * @param id ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除版本")
    @ApiImplicitParam(name = "id", value = "版本id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        sysUpgradeService.deleteByPrimaryKey(id);
        return JsonForResult.builder().success(true).build();
    }

    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    })
    public JsonForResult<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = sysUpgradeService.uploadFile(file);
            return JsonForResult.<Map<String, Object>>builder().data(result).success(true).build();
        } catch (Exception e) {
            throw new EpiCloudException("安装包上传失败！ " + e.getMessage());
        }
    }

}
